<template>
  <el-dialog title="加入实验" width="450px" :visible.sync="show">
    <el-row :gutter="24">
      <el-col :span="16">
        <el-input placeholder="请输入实验编码" v-model="code"></el-input>
      </el-col>
      <el-col :span="8" class="center_x" style="display: flex;flex-direction: column-reverse;">
        <el-button @click="send()" type="primary">加入实验</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import CApi from "../../api/Course.js";
import RApi from "../../api/ExpRecord.js";
export default {
  props: {
    visible: Boolean
  },
  watch: {
    visible: function(val) {
      this.show = val;
    },
    show: function(val) {
      this.$emit("update:visible", val);
    }
  },
  data() {
    return {
      show: false,
      code: ""
    };
  },
  methods: {
    send() {
      //todo
      this.axios
        .get(CApi.findCourseByCode, { params: { experimentCode: this.code } })
        .then(res => {
          if (res.data.status === 0) {
            if (res.data.data) {
              let obj = res.data.data;
              //todo bug待补充。
              this.axios
                .post(RApi.add, {
                  params: 2,
                  id:obj.id,
                  username: this.$store.state.user.username,
                  school: this.$store.state.user.school,
                  experimentName: obj.experimentName,
                  courseName: obj.courseName,
                })
                .then(res => {
                  if (res.data.status == 0) {
                    localStorage.setItem("exId", res.data.data); //实验记录Id
                    localStorage.setItem("userId", this.$store.state.user.id);
                    localStorage.setItem(
                      "school",
                      this.$store.state.user.school
                    );
                    localStorage.setItem(
                      "username",
                      this.$store.state.user.username
                    );
                    localStorage.setItem("group", "班级");
                    localStorage.setItem("teacher", obj.username);
                    localStorage.setItem("experimentName", "分散实验");
                  }
                  window.location.href = "/zsd/webgl/";
                });
            } else {
              alert("不存在该编号的课程");
            }
          }
        });
    }
  }
};
</script>

<style>
</style>
